package com.example.demo.service;

import com.example.demo.mapper.CommonImageMapper;
import com.example.demo.mapper.SchoolClassMapper;
import com.example.demo.mapper.SchoolMapper;
import com.example.demo.model.Common;
import com.example.demo.model.CommonImage;
import com.example.demo.model.SchoolClass;
import com.github.pagehelper.PageHelper;

import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.Date;
import java.util.List;

@Service
public class SchoolService {

	@Autowired
	private SchoolMapper schoolMapper;
	@Autowired
	private CommonImageMapper commonImageMapper;
	@Autowired
	private SchoolClassMapper schoolClassMapper;

	public List<Common> selectSchools(@PathVariable("cityId") Integer cityId,
			@PathVariable("districtId") Integer districtId, @PathVariable("minprice") Double minprice,
			@PathVariable("maxprice") Double maxprice, @PathVariable("drivingModelId") Integer drivingModelId,
			@PathVariable("pageIndex") Integer pageIndex, @PathVariable("pageSize") Integer pageSize) {
		// 单表分页
		PageHelper.startPage(pageIndex, pageSize);

		// 单表自定义查询
		Example example = new Example(Common.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("type", 2);

		if (districtId != 0) {
			criteria.andEqualTo("belongId", districtId);
		} else if (cityId != 0) {
			criteria.andEqualTo("teachingAge", cityId);
		}

		if (minprice > 0) {

		}

		if (drivingModelId > 0) {
			criteria.andLike("tag", drivingModelId.toString());
		}

		return schoolMapper.selectByExample(example);
	}

	public Common selectSchool(@PathVariable("id") Integer id) {
		// 单表自定义查询
		/*Example example = new Example(Common.class);
		Criteria criteria = example.createCriteria();*/
		Common model = schoolMapper.selectByPrimaryKey(id);

		Example exampleImage = new Example(CommonImage.class);
		Criteria criteriaImage = exampleImage.createCriteria();
		criteriaImage.andEqualTo("fromId", id);

		List<CommonImage> images = commonImageMapper.selectByExample(exampleImage);

		model.setImages(images);
		return model;
	}

	/*获取驾校即将开班班级信息,按照日期从近到远排序*/
	public List<SchoolClass> getSchoolClassesAtOnceBySchool(@PathVariable("schoolId") Integer schoolId,
			@PathVariable("pageIndex") Integer pageIndex, @PathVariable("pageSize") Integer pageSize) {
		// 单表分页
		PageHelper.startPage(pageIndex, pageSize);

		// 单表自定义查询
		Example example = new Example(SchoolClass.class);
		Criteria criteria = example.createCriteria();
		//criteria.andEqualTo("fromId", schoolId);
		criteria.andGreaterThan("startTime", new Date()).andEqualTo("schoolId", schoolId);
		example.setOrderByClause(" start_time asc ");
		return schoolClassMapper.selectByExample(example);
	}

}
